package com.kaizzi.data.jdl.controller;

import com.kaizzi.data.common.result.Result;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.jdl.entity.LogHistory;
import com.kaizzi.data.jdl.service.DownloadService;
import com.kaizzi.data.jdl.service.JdlDataService;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.CreateJdlPPT;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/9
 */
@RestController
@RequestMapping("/jdl")
public class JdlDataController {

    @Resource
    private JdlDataService jdlDataService;
    @Resource
    private DownloadService downloadService;
    @Resource
    private CreateJdlPPT createJdlPPT;
    @Resource
    private LogService logService;

    //获取具体page的数据信息
    @ApiOperation(value = "获取具体page的数据信息", notes = "获取具体page的数据信息", httpMethod = "GET")
    @GetMapping("/getPageData")
    public Result getPageData(@RequestParam("page") String page){
        String pageData = jdlDataService.getPageData(page);
        return Result.getSuccess("SUCCESS",pageData);
    }

    //获取本地已经缓存上周数据数量
    @ApiOperation(value = "获取本地已经缓存上周数据数量", notes = "获取本地已经缓存上周数据数量", httpMethod = "GET")
    @GetMapping("/getDataStatus")
    public Result getLastSundayDataStatus(){
        int size = jdlDataService.getDataStatus();
        if (size == 30){
//            "JDL应有数据30条，当前已缓存"+size+"条"
            return Result.getSuccess("success");
        }
        return Result.getFailed("fail");
    }

    @ApiOperation(value = "获取状态信息", notes = "获取状态信息", httpMethod = "GET")
    @GetMapping("/getStatus")
    public Result getStatus(){
        LogHistory logHistory = logService.getById(896);
        if (logHistory != null && logHistory.getDataTime().equals(DateUtil.getLastSundayFormat())){
            return Result.getSuccess(logHistory.getStatus());
        }
        return Result.getFailed("fail");
    }

    //重新生成ppt
    @ApiOperation(value = "手动重新生成ppt", notes = "手动重新生成ppt", httpMethod = "GET")
    @GetMapping("/createPPT")
    public Result createPPT(){
        createJdlPPT.createPPT("jdl.pptx","temp1118.pptx");
        return Result.getSuccess("1");
    }

    /**
     * excel文件上传
     * @param file 文件
     * @return 返回上传成功信息
     */
    @ApiOperation(value = "excel文件上传", notes = "excel文件上传", httpMethod = "POST")
    @PostMapping("/jdlExcelUpload")
    public Result handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                //文件保存到本地
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(
                                new File("jdlExcel.xlsx")));
                out.write(file.getBytes());
                out.flush();
                out.close();

                //文件上传成功后解析excel并生成相应数据 开启异步线程处理
                new Thread(() -> jdlDataService.analyticalExcelData()).start();

            } catch (IOException e) {
                e.printStackTrace();
                return Result.getFailed(e.getMessage());
            }
            return Result.getSuccess("SUCCESS");
        } else {
            return Result.getFailed("FAIL BY EMPTY FILE");
        }
    }

    //jdl.pptx文件下载
    @ApiOperation(value = "jdl.pptx文件下载", notes = "jdl.pptx文件下载", httpMethod = "GET")
    @RequestMapping("/jdlDownload")
    public void download(HttpServletRequest request, HttpServletResponse response){
        downloadService.download(request,response);
    }
}
